<template>
  <div>
    <el-dialog
      title="赋分"
      width="530px"
      :visible.sync="dialogVisible"
      :before-close="handleClose"
      :close-on-click-modal="false"
    >
      <div class="dialogBody">
        <div class="formBox">
          <el-form
            :model="params"
            :rules="rules"
            inline
            ref="ruleForm"
            label-position="right"
            label-width="140px"
            size="small"
          >
            <!-- <div class="searchBox">
              <el-form-item label="请输入选择居民：" prop="input">
                <el-autocomplete
                  popper-class="my-autocomplete"
                  v-model="params.input"
                  :fetch-suggestions="querySearchAsync"
                  placeholder="请输入"
                  size="small"
                  @select="handleSelect"
                >
                  <template slot-scope="{ item }">
                    <div v-if="item.id == -1">无匹配结果</div>
                    <div v-else>
                      <div class="name">{{ item.userName }}</div>
                    </div>
                  </template>
                </el-autocomplete>
              </el-form-item>
            </div>
            <div class="userInfo" v-if="infoShow">
              <div class="addr">联系电话：{{ userInfo.mobile }}</div>
              <div class="addr">居住房屋：{{ userInfo.house.name }}</div>
              <div class="addr">身份证号：{{ userInfo.idNo }}</div>
            </div> -->

            <el-form-item label="积分数量：" prop="point">
              <el-input
                oninput="value=value.replace(/[^\d]/g, '').replace(/(\d*)(\.?)(\d*).*/, '$1')"
                v-model="params.point"
                @blur="params.point = $event.target.value"
              ></el-input>
            </el-form-item>
            <el-form-item label="赋分说明：" prop="remarks">
              <el-input
                type="textarea"
                v-model="params.remarks"
                :rows="4"
              ></el-input>
            </el-form-item>
          </el-form>
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="handleClose">取 消</el-button>
        <el-button size="small" type="primary" @click="onSubmit('ruleForm')"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { userList } from "@/api/activity.js";
import { addUserPoint } from "@/api/score.js";
export default {
  name: "",
  props: ["dialogVisible", "id"],
  data() {
    return {
      userInfo: {
        house: {},
      },
      infoShow: false,
      params: { uid: "", point: "", remarks: "" },
      rules: {
        point: [{ required: true, message: "请输入积分数", trigger: "blur" }],
        remarks: [
          { required: true, message: "请输入赋分说明", trigger: "blur" },
        ],
      },
    };
  },

  components: {},

  computed: {},

  beforeMount() {},

  created() {
    this.params.uid = this.id;
  },
  mounted() {},

  methods: {
    async querySearchAsync(queryString, cb) {
      if (queryString && queryString.length > 0) {
        this.input = queryString;
        try {
          const data = await userList({
            keywords: this.input,
          }); //search定义在data里
          console.log(data);
          // 赋值给建议列表，渲染到页面
          let list = data.data;
          // 如果list.length等于0，则代表没有匹配到结果。手动给list添加一条提示信息
          if (list.length == "0") {
            list.push({
              id: "-1",
              value: "无匹配结果",
            });
            // 调用 callback 返回建议列表的数据
            cb(list);
          } else {
            // list = list.map((item) => {
            //   return {
            //     value: `${item.userName}`,
            //     id: `${item.id}`,
            //   };
            // });
            // 调用 callback 返回建议列表的数据
            cb(list);
          }
        } catch (error) {
          console.log(error);
        }
      } else {
        cb([]);
      }
    },
    handleSelect(val) {
      this.params.input = val.userName;
      this.userInfo = val;
      this.infoShow = true;
    },
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          addUserPoint(this.params).then((res) => {
            if (res.code == 200) {
              this.$message({
                message: "赋值成功",
                type: "success",
              });
              this.$emit("handleClose", 1);
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    handleClose() {
      this.$emit("handleClose");
    },
  },

  watch: {},
};
</script>
<style lang="scss" scoped>
.dialogBody {
  .userInfo {
    margin: 10px 0 20px 140px;
    line-height: 24px;
  }
  .el-autocomplete {
    width: 90%;
  }
}
</style>
<style lang="scss">
.my-autocomplete {
  li {
    line-height: 20px;
    padding: 7px;
    > div {
      text-overflow: ellipsis;
      overflow: hidden;
    }

    .addr {
      font-size: 14px;
      color: #999;
    }
  }
}
.formBox {
  margin-top: 20px;
  .el-input__inner {
    width: 300px;
  }
  .el-textarea__inner {
    width: 300px;
  }
}
</style>
